
#include <iostream>
#include <string>
#include <cmath>
#include <sstream>

#include <cairo.h>
#include <lgi/x11window.hpp>
#include <lgi/layer.hpp>

#include "joystick.h"
#include "metalwidgets.h"
#include "serial.h"
#include "peterbus.h"

using namespace std;
using namespace net::lliurex::lgi;

bool quit_request=false;






class Cockpit: public Layer
{
	public:
	
		LCD * lcd;
		LED * led1;
		LED * led2;
		Manifold * manifold1;
		Button * button1;
		Button * button2;
		Panic * panic;
		Slider * slider1;
		TvScreen * tvscreen1;
		Label * lbl1;
		
		
		Cockpit() : Layer("Cockpit")
		{
			MetalWidgets::LoadResources();
			
			lcd = new LCD(0,0);
			lcd->text1="Hello world";
			lcd->text2="Mosfet 1 overheating";
			
			led1 = new LED(0,64,"TEMP 1");
						
			led2 = new LED(64,64,"ERROR");
			led2->status=true;
			
			manifold1 = new Manifold(128,64);
			
			button1 = new Button(264,0,"ENG1");
			button2 = new Button(264,64,"E2");
			button2->status=true;
			
			panic = new Panic(512,0);
			
			slider1 = new Slider(328,0,"ENG1");
			
			tvscreen1 = new TvScreen(0,128,"XY-ACCEL");
			
			lbl1 = new Label(256,512,"PWM motors");
			
			Add(lbl1);
			Add(tvscreen1);
			Add(slider1);
			Add(panic);
			Add(lcd);
			Add(led1);
			Add(led2);
			Add(manifold1);
			Add(button1);
			Add(button2);
		}
		
		~Cockpit()
		{
			
		}
		
		void OnDestroy(DestroyEvent * event)
		{
			cout<<"Cockpit::Destroy"<<endl;
			quit_request=true;
		}
		
		void OnKeyPress(KeyPressEvent * event)
		{
			if(event->key==LGI_KEY_s)
			{
				cout<<"* requesting rpm..."<<endl;
				unsigned char cmd = 0x01;
				pb_write(0x02,1,&cmd);
			}
				
			//cout<<"Cockpit::KeyPress "<<event->key<<endl;
			if(focus!=NULL)
			{
				if(event->key==LGI_KEY_UP)focus->y-=1.0f;
				if(event->key==LGI_KEY_DOWN)focus->y+=1.0f;
				if(event->key==LGI_KEY_LEFT)focus->x-=1.0f;
				if(event->key==LGI_KEY_RIGHT)focus->x+=1.0f;
				
				
			}
			
			
		}

};


int main(int argc,char * argv[])
{
	X11Window window;
	Cockpit * cockpit;
	cairo_t * cairo;
	
	stringstream ss;
	float value;
	
	unsigned char buffer[64];
	unsigned char len;
	unsigned char id;
	
	
	cout<<"* starting arduino comm..."<<endl;
	pb_init();
	
	cout<<"* Starting peterquadcopter cockpit systems..."<<endl;
	
	window.Create(800,600,LGI_FLAG_CENTERED);

	cockpit = new Cockpit();
	window.AddLayer(cockpit);
	
	cout<<"* Ready"<<endl;
	//Joystick joystick("/dev/input/js1");
	
	while(!quit_request)
	{
		window.DispatchEvents(LGI_DISPATCH_EVENTS_FULL);
		
		
			
		if(pb_read(&id,&len,buffer))
		{
			if(id==0x01)
			{
				int rpm1 = buffer[0] | (buffer[1]<<8);
				int rpm2 = buffer[2] | (buffer[3]<<8);
				ss.str("");
				ss<<"RPM1: "<<dec<<rpm1;
				cockpit->lcd->text1=ss.str();
				
				ss.str("");
				ss<<"RPM2: "<<dec<<rpm2;
				cockpit->lcd->text2=ss.str();
				
				value = rpm2/1024.0f;
				cockpit->manifold1->value=value;
			}
		}
					
			
		
		
		/*
		joystick.Update();
		
		cout<<"joystick button 0"<<(int)joystick.buttons[0]<<endl;
		cout<<"joystick x:"<<joystick.axis[0]<<endl;
		cout<<"joystick y:"<<joystick.axis[1]<<endl;
		cout<<"joystick z:"<<joystick.axis[2]<<endl;
		
		cockpit->manifold1->value=(1.0f+joystick.axis[2])/2.0f;
		*/
		cairo = window.cairo;
		
		cairo_set_source_rgb(cairo,0.0,0.0,0.0);
		cairo_paint(cairo);
		window.Flip();
	}
	
	
	pb_close();
	window.Destroy();
	
	delete cockpit;
	
	cout<<"* Bye"<<endl;
	
	return 0;
}
